//
//  InteractiveViewController.m
//  suRead
//
//  Created by lidi on 15/11/10.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "InteractiveViewController.h"
#import "Masonry.h"
#import "UIViewController+HUD.h"
#import "UIControl+Blocks.h"
#import "MJRefresh.h"
#import "ServiceNews.h"
#import "M_News.h"

#import "QRViewController.h"
#import "ServingViewController.h"

#import "NewCellManager.h"
#import "AppDelegate.h"
#import "UIViewController+EmptyDataSet.h"

@interface InteractiveViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,weak) IBOutlet UITableView *infoTableView;
@property (nonatomic,strong) NSMutableArray *infoDataArray;
@property (nonatomic,strong)NSURLSessionDataTask *requestHandle;
@property (nonatomic,assign)NSInteger currentPageIndex;
@end

@implementation InteractiveViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // enable slide-back
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    // Do any additional setup after loading the view.
    __weak typeof(self) weak_self = self;
    
    UIView *topBarView = [UIView new];
    topBarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topBarView];
    [topBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(topBarView.superview);
        if (iPhoneX) {
            make.top.equalTo(topBarView.superview).offset(44.0f);
        } else {
            make.top.equalTo(topBarView.superview).offset(20.0f);
        }
        make.height.equalTo(@44.0f);
    }];
    
    //融媒体封面
    UIButton *buttonCover = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonCover setImage:[UIImage imageNamed:@"button_cover"] forState:UIControlStateNormal];
    [buttonCover setTitle:@"  融媒体封面" forState:UIControlStateNormal];
    buttonCover.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [buttonCover setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonCover addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        AppDelegate *delegate_ = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [delegate_ showOpenView];
    }];
    [topBarView addSubview:buttonCover];
    [buttonCover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(buttonCover.superview);
        make.left.equalTo(buttonCover.superview).offset(15.0f);
    }];
    
    //服务
    UIButton *buttonServices = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonServices setImage:[UIImage imageNamed:@"button_server"] forState:UIControlStateNormal];
    [buttonServices setTitle:@"  服务" forState:UIControlStateNormal];
    buttonServices.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [buttonServices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonServices addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        ServingViewController *controller = [ServingViewController new];
        controller.hidesBottomBarWhenPushed = YES;
        [weak_self showViewController:controller sender:nil];
    }];
    [topBarView addSubview:buttonServices];
    [buttonServices mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(buttonServices.superview);
        make.right.equalTo(buttonServices.superview).offset(-15.0f);
    }];
    
    //扫码
    UIButton *buttonScan = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonScan setImage:[UIImage imageNamed:@"button_scan"] forState:UIControlStateNormal];
    [buttonScan setTitle:@"  扫码" forState:UIControlStateNormal];
    buttonScan.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [buttonScan setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonScan addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        QRViewController *controller = [QRViewController new];
        controller.hidesBottomBarWhenPushed = YES;
        [weak_self showViewController:controller sender:nil];
    }];
    [topBarView addSubview:buttonScan];
    [buttonScan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(buttonScan.superview);
        make.right.equalTo(buttonServices.mas_left).offset(-15.0f);
    }];
    
    self.infoTableView.tableFooterView = [UIView new];
    self.infoTableView.backgroundColor = [[UIColor alloc] initWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    self.infoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [NewCellManager registerCellFor:self.infoTableView];
    
    self.infoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //刷新数据
        [weak_self refreshDatas];
    }];
    
    self.infoTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weak_self loadMore];
    }];
    
    [self.infoTableView.mj_header beginRefreshing];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self setNeedsStatusBarAppearanceUpdate];
    
    //2016-6-22 sah hold left menu viewDidAppear
    NSString* myopen = [AppDelegate sharedAppDelegate].myOpen;
    if([myopen isEqual:@"1"]) {
        //师爱华 -2016-6-22
        [AppDelegate sharedAppDelegate].myOpen=@"0";
        //[[AppDelegate sharedAppDelegate].sideMenuViewController presentLeftMenuViewController];
    }
    
}

- (void)refreshDatas
{
    [self emptyDataSetClearAllNotice];
    
    self.currentPageIndex = 1;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doFetchInteractiveList:self.currentPageIndex pageSize:DEFAULT_PAGE_SIZE success:^(id responseObject, NSString *message) {
        [weak_self.infoTableView.mj_header endRefreshing];
        weak_self.infoDataArray = [NSMutableArray array];
        [weak_self.infoDataArray addObjectsFromArray:responseObject];
        [weak_self.infoTableView reloadData];
    } failure:^(NSString *message) {
        [weak_self.infoTableView.mj_header endRefreshing];
        [weak_self showHUDText:message];
        //重连
        if (!weak_self.infoDataArray || ![weak_self.infoDataArray count]) {
            [weak_self emptyDataSetShowNetWorkAvaliable:^{
                [weak_self.infoTableView.mj_header beginRefreshing];
            }];
        }
    }];
}


- (void)loadMore
{
    self.currentPageIndex ++;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doFetchInteractiveList:self.currentPageIndex pageSize:DEFAULT_PAGE_SIZE success:^(id responseObject, NSString *message) {
        [weak_self.infoTableView.mj_footer endRefreshing];
        [weak_self.infoDataArray addObjectsFromArray:responseObject];
        [weak_self.infoTableView reloadData];
    } failure:^(NSString *message) {
        [weak_self.infoTableView.mj_footer endRefreshing];
        [weak_self showHUDText:message];
    }];
}


#pragma mark tableViewDatasouce Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.infoDataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_News *news = [self.infoDataArray objectAtIndex:indexPath.row];
    return [NewCellManager cellForNews:news whos:0 tableView:tableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_News *news = [self.infoDataArray objectAtIndex:indexPath.row];
    return [NewCellManager heightForNewsCell:news whos:0];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    M_News *news_ = [self.infoDataArray objectAtIndex:indexPath.row];
    [NewCellManager jumpToNewsDetail:news_ wohs:0 handle:self];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

@end
